﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using BL.Client.BudgetManageBL;
using Common.IO.FileIO;

namespace UI.BudgetManage.ChildForm.LocalBudgetReport
{
    public partial class FileBudgetReport : Form
    {
        BudgetManageServerice BMS;
        string ExcelFilePath;
        public FileBudgetReport()
        {
            InitializeComponent();
        }

        private void btnBrower_Click(object sender, EventArgs e)
        {
            this.ExcelFilePath = null;
            ofdExcelFile.FileOk += new CancelEventHandler(ofdExcelFile_FileOk);
            ofdExcelFile.DefaultExt = ".xls";
            ofdExcelFile.Filter = "Excel文件(*.xls)|*.xls";
            ofdExcelFile.CheckFileExists = true;
            ofdExcelFile.FileName = "";
            ofdExcelFile.ShowDialog();

            if(this.ExcelFilePath == null)
                return;

            ExcelIndentify ei = new ExcelIndentify(ExcelFilePath);
            string express = ".*-.*-.*-预算上报";
            string[] columnnams = { "预算类别", "预算子类别", "预算名称", "预算总金额", "开支方式", "预算备注" };
            string department_name, department_parent_name, user_name;
            IndentifyResult ir = ei.FileIndentify(express, columnnams);
            if (ir == IndentifyResult.ExcelNotExist)
            {
                MessageBox.Show("文件不存在！请检查！");
                return;
            }
            else if (ir == IndentifyResult.ExcelNameWrong)
            {
                MessageBox.Show("文件名不正确！无法识别！");
                return;
            }
            else if (ir == IndentifyResult.ExcelFormatWrong)
            {
                MessageBox.Show("Excel内格式不规范！");
                return;
            }
            else if (ir == IndentifyResult.IndenfitySuccess)
            {
                string[] filename = new System.IO.FileInfo(ExcelFilePath).Name.Split('-');
                department_parent_name = filename[0];
                department_name = filename[1];
                user_name = filename[2];
                tbDeptPName.Text = department_parent_name;
                tbDeptName.Text = department_name;
                tbUserName.Text = user_name;
                ExcelIO excel = new ExcelIO(ExcelFilePath);
                dgvExcelData.DataSource = excel.GetData().Tables[0];

                lbText.ForeColor = System.Drawing.Color.DarkGreen;
                lbText.Text = "文件识别成功。";
            }

        }

        private void ofdExcelFile_FileOk(object sender, CancelEventArgs e)
        {
            this.ExcelFilePath = ofdExcelFile.FileName;
            tbExcelFilePath.Text = ExcelFilePath;
        }

        private void FileBudgetReport_Load(object sender, EventArgs e)
        {
            this.BMS = ((BMMain)this.MdiParent).BMS;
        }

        private void btnBudgetReport_Click(object sender, EventArgs e)
        {
            string user_name = tbUserName.Text.Trim();
            OperateResult or = BMS.BudgetReport((DataTable)dgvExcelData.DataSource, user_name);
            if (or == OperateResult.Success)
                MessageBox.Show("上报成功！");
            else if(or == OperateResult.Fail)
                MessageBox.Show("上报失败！请检查文件内容是否正确！");
            else if(or == OperateResult.DisConnect)
                MessageBox.Show("请检查网络连接！");
        }
    }
}